package com.yanqu.road.server.cmd.activity.stronghold.eliminate;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.stronghold.config.StrongholdTradeConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityRankMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Map;

@Cmd(code = GameProtocol.S_STRONGHOLD_TRADE, desc = "西域通商")
public class StrongholdTradeCmd extends AbstractCommand {
    private int id;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.StrongholdTradeReqMsg req = StrongholdWarProto.StrongholdTradeReqMsg.parseFrom(packet.getBytes());
        id = req.getId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
            return;
        }
        StrongholdTradeConfig tradeConfig = StrongholdActivityMgr.getTradeConfig(id);
        if (tradeConfig == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
            return;
        }
        int activityId = activityInfo.getActivityId();
        boolean canTrade = false;
        UnionActivityConditionData unionActivityConditionData = StrongholdActivityRankMgr.getUnionActivityConditionData(activityId, StrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_UNION, player.getUserId());
        if (unionActivityConditionData != null) {
            int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
            UnionActivityRankProto.UnionActivityRankListMsg rankList = CrossDataMgr.getCrossActivityUnionConditionTypeRankList(activityId, groupId, StrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_UNION);
            if (rankList != null && rankList.getRankListCount() > 0) {
                int myrank = 0;
                int rank = 0;
                for (UnionActivityRankProto.UnionActivityRankTempMsg tempMsg : rankList.getRankListList()) {
                    rank++;
                    if (tempMsg.getUnionUid().equals(unionActivityConditionData.getUnionUid())) {
                        myrank = rank;
                        break;
                    }
                }
                String[] split = tradeConfig.getRankRange().split("\\|");
                int beginRank = Integer.parseInt(split[0]);
                int endRank = Integer.parseInt(split[1]);
                if (myrank >= beginRank && myrank <= endRank) {
                    canTrade = true;
                }
            }
        }
        if (!canTrade) {
            ret = GameErrorCode.E_ERROR_DATA;
            return;
        }

        String consume = tradeConfig.getConsume();
        Property property = PropertyHelper.parseStringToProperty(consume);
        if (!player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.Stronghold, eLogMoneyType.StrongholdTrade)) {
            ret = GameErrorCode.E_BAG_ITEM_NO_EXIST;
            return;
        }
        StrongholdWarMgr.addTrade(activityInfo.getActivityId(), player.getUserId(), player.getUserInfo().getUnionUid(), id, tradeConfig.getRewards());

        player.getModule(StrongholdModule.class).syncUserData();
    }

    @Override
    public void sendData(GamePlayer player) {
        StrongholdWarProto.StrongholdTradeRespMsg.Builder resp = StrongholdWarProto.StrongholdTradeRespMsg.newBuilder();
        resp.setRet(ret);
        player.sendPacket(ClientProtocol.U_STRONGHOLD_TRADE, resp);
    }
}
